class_name  Player

extends CharacterBody2D
@onready var 蓝色坦克: Sprite2D = $蓝色坦克
@onready var 红色坦克: Sprite2D = $红色坦克
@onready var 子弹出生坐标点: Node2D = $子弹出生坐标点
@onready var inputcmd: inputCmd = $inputcmd
@onready var 子弹间隔=$"子弹间隔"
var 发射子弹许可:bool=false
const 子弹b:PackedScene = preload("uid://b1tkvgtr6oy3u")
const SPEED = 350.0
var  SpeedV2:Vector2 = Vector2(50.0,50.0)
var P_rotation=0#旋转角度
const JUMP_VELOCITY = 300
const 出生点=Vector2(100,100)
var life:bool=true
func _enter_tree() -> void:
	print("控制权限归属ID进入场景树前：%s"%name)
	position=出生点
func _ready() -> void:
	if (name.to_int())!=1:#设置坦克颜色
		蓝色坦克.visible=false
		红色坦克.visible=true
	set_multiplayer_authority(1)
	print("控制权限归属ID_reday：%s"%get_multiplayer_authority())
	if 红色坦克.visible:
		print("控制权限归属 红色坦克")
	if 蓝色坦克.visible:
		print("控制权限归属 蓝色坦克")
func _physics_process(_delta: float) -> void:
	if not life:return
	if not multiplayer.is_server():return
		
	move(_delta)
	if inputcmd.inputCmd_atk:
		发射子弹函数.rpc()
func _process(_delta: float) -> void:	
	if not life:return
	if not multiplayer.is_server():return


		
	
		
	
	


func _on_player_hit_box_hit() -> void:
	pass # Replace with function body.


func _on_player_hurt_box_hurt(_name) -> void:#被打击后执行函数
	if str(name)=="bullet":
		life=false
		print("name")
	
	pass # Replace with function body.
	
@rpc("authority","call_local","reliable")
func 发射子弹函数()-> void:
	if not 发射子弹许可:
		return
	if not 子弹间隔.is_stopped():
		return
	子弹间隔.start()
	inputcmd.inputCmd_atk=false
	var create子弹:子弹2=子弹b.instantiate() as 子弹2
	create子弹.获取发射方向(Vector2(cos(get_rotation()), sin(get_rotation())))
	create子弹.position=子弹出生坐标点.global_position
	get_parent().add_child(create子弹,true)
	
func move(_delta):#玩家移动
	if inputcmd.inputCmd_vector2.length_squared()>0.16:
		
		velocity =inputcmd.inputCmd_vector2 * SPEED
	#if inputcmd.inputCmd_vector2.abs()>Vector2(0.3,0.3):
	if velocity:
		velocity= velocity.move_toward(Vector2.ZERO, 23)
	if inputcmd.inputCmd_vector2!=Vector2.ZERO:
		rotation=inputcmd.inputCmd_vector2.angle()
	move_and_slide()
	
